<?php

namespace App\Http\Controllers;

use App\Classes\ThirdOauth\ThirdOauth;
use App\Services\ApplicantService;
use App\Services\ThirdPartyService;
use App\Services\UserService;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;

class ThirdController extends Controller
{
    public function twitterCallback(Request $request, ThirdPartyService $service)
    {
        $referer = array_get($request->session()->get('_previous'),'url');

        $return = $service->thirdCallback('twitter',['state'=> $referer]);
        if(!$return){
            $url_arr = $service->returnFalse($referer);
            return redirect($url_arr['url'].'?'.http_build_query($url_arr['params']));
        }
        $url = $service->getRedirectCallbackUri();
        $return = $service->getReturnParams();
        \Log::info('google_test',[$url,$return]);
        return redirect($url.'?'.http_build_query($return));
    }

    public function facebookCallback(Request $request, ThirdPartyService $service)
    {
        $state = $request->input('state');
        $return = $service->thirdCallback('facebook',['state'=>$state]);

        if(!$return){
            $url_arr = $service->returnFalse($state);
            return redirect($url_arr['url'].'?'.http_build_query($url_arr['params']));
        }

        $url = $service->getRedirectCallbackUri();
        $return = $service->getReturnParams();
        \Log::info('google_test',[$url,$return]);
        return redirect($url.'?'.http_build_query($return));
    }

    public function googleCallback(Request $request, ThirdPartyService $service)
    {
        $state = $request->input('state');
        $return = $service->thirdCallback('google',['state'=>$request->input('state')]);

        if(!$return){
            $url_arr = $service->returnFalse($state);
            return redirect($url_arr['url'].'?'.http_build_query($url_arr['params']));
        }

        $url = $service->getRedirectCallbackUri();
        $return = $service->getReturnParams();
        \Log::info('google_test',[$url,$return]);
        return redirect($url.'?'.http_build_query($return));
    }


    public function confirmEmail(Request $request, ThirdPartyService $service, UserService $userService)
    {
        $email = $request->input('email','');
        $provider_id = $request->input('provider_id','');
        if($userService->isExistEmail($email)){
            if(!$service->isExistProviderId($provider_id)){
                //外部id有误
                dd(false);
            }else{
                $user_id = $userService->registerUser($email,$provider_id);
                //登录
                dd('登录成功');
            }
        }else{
            if($service->isExistProviderId($provider_id)){
                $service->updateProviderEmail($email,$provider_id);
            }else{
                $service->addThirdParty($email,$provider_id);
            }
            $user_id = $userService->registerUser($email,$provider_id);
            dd('登录成功');
        }

    }
}
